#include "motor.h"

void motor_init()
{
	__HAL_TIM_SET_COMPARE(&M1_TIMER,M1_CHANNEL,0);
	__HAL_TIM_SET_COMPARE(&M2_TIMER,M2_CHANNEL,0);
	__HAL_TIM_SET_COMPARE(&M3_TIMER,M3_CHANNEL,0);
	__HAL_TIM_SET_COMPARE(&M4_TIMER,M4_CHANNEL,0);
	__HAL_TIM_SET_COMPARE(&M5_TIMER,M5_CHANNEL,0);
	__HAL_TIM_SET_COMPARE(&M6_TIMER,M6_CHANNEL,0);
	__HAL_TIM_SET_COMPARE(&M7_TIMER,M7_CHANNEL,0);
	__HAL_TIM_SET_COMPARE(&M8_TIMER,M8_CHANNEL,0);
	HAL_TIM_PWM_Start(&M1_TIMER, M1_CHANNEL);
	HAL_TIM_PWM_Start(&M2_TIMER, M2_CHANNEL);
	HAL_TIM_PWM_Start(&M3_TIMER, M3_CHANNEL);
	HAL_TIM_PWM_Start(&M4_TIMER, M4_CHANNEL);
	HAL_TIM_PWM_Start(&M5_TIMER, M5_CHANNEL);
	HAL_TIM_PWM_Start(&M6_TIMER, M6_CHANNEL);
	HAL_TIM_PWM_Start(&M7_TIMER, M7_CHANNEL);
	HAL_TIM_PWM_Start(&M8_TIMER, M8_CHANNEL);
}


void motor_deinit()
{
	__HAL_TIM_SET_COMPARE(&M1_TIMER,M1_CHANNEL,0);
	__HAL_TIM_SET_COMPARE(&M2_TIMER,M2_CHANNEL,0);
	__HAL_TIM_SET_COMPARE(&M3_TIMER,M3_CHANNEL,0);
	__HAL_TIM_SET_COMPARE(&M4_TIMER,M4_CHANNEL,0);
	__HAL_TIM_SET_COMPARE(&M5_TIMER,M5_CHANNEL,0);
	__HAL_TIM_SET_COMPARE(&M6_TIMER,M6_CHANNEL,0);
	__HAL_TIM_SET_COMPARE(&M7_TIMER,M7_CHANNEL,0);
	__HAL_TIM_SET_COMPARE(&M8_TIMER,M8_CHANNEL,0);
	HAL_TIM_PWM_Stop(&M1_TIMER, M1_CHANNEL);
	HAL_TIM_PWM_Stop(&M2_TIMER, M2_CHANNEL);
	HAL_TIM_PWM_Stop(&M3_TIMER, M3_CHANNEL);
	HAL_TIM_PWM_Stop(&M4_TIMER, M4_CHANNEL);
	HAL_TIM_PWM_Stop(&M5_TIMER, M5_CHANNEL);
	HAL_TIM_PWM_Stop(&M6_TIMER, M6_CHANNEL);
	HAL_TIM_PWM_Stop(&M7_TIMER, M7_CHANNEL);
	HAL_TIM_PWM_Stop(&M8_TIMER, M8_CHANNEL);
}




void motor_set_speed(uint8_t motor,uint16_t speed)
{
	if(speed<MIN_SPEED)
	{
		speed=0;
		switch(motor)
		{
			case 1 : __HAL_TIM_SET_COMPARE(&M1_TIMER,M1_CHANNEL,speed);
					 HAL_TIM_PWM_Stop(&M1_TIMER, M1_CHANNEL);
					 break;
			case 2 : __HAL_TIM_SET_COMPARE(&M2_TIMER,M2_CHANNEL,speed);
					HAL_TIM_PWM_Stop(&M2_TIMER, M2_CHANNEL);
					break;
			case 3 : __HAL_TIM_SET_COMPARE(&M3_TIMER,M3_CHANNEL,speed);
					HAL_TIM_PWM_Stop(&M3_TIMER, M3_CHANNEL);
					break;
			case 4 : __HAL_TIM_SET_COMPARE(&M4_TIMER,M4_CHANNEL,speed);
					HAL_TIM_PWM_Stop(&M4_TIMER, M4_CHANNEL);
					break;
			case 5 : __HAL_TIM_SET_COMPARE(&M5_TIMER,M5_CHANNEL,speed);
					HAL_TIM_PWM_Stop(&M5_TIMER, M5_CHANNEL);
					break;
			case 6 : __HAL_TIM_SET_COMPARE(&M6_TIMER,M6_CHANNEL,speed);
					HAL_TIM_PWM_Stop(&M6_TIMER, M6_CHANNEL);
					break;
			case 7 : __HAL_TIM_SET_COMPARE(&M7_TIMER,M7_CHANNEL,speed);
					HAL_TIM_PWM_Stop(&M7_TIMER, M7_CHANNEL);
					break;
			case 8 : __HAL_TIM_SET_COMPARE(&M8_TIMER,M8_CHANNEL,speed);
					HAL_TIM_PWM_Stop(&M8_TIMER, M8_CHANNEL);
					break;
			default : break;
		}


	}
	else
	{
		if(speed>MAX_SPEED) speed=MAX_SPEED;
		switch(motor)
		{
			case 1 : __HAL_TIM_SET_COMPARE(&M1_TIMER,M1_CHANNEL,speed);
					HAL_TIM_PWM_Start(&M1_TIMER, M1_CHANNEL);
					break;
			case 2 : __HAL_TIM_SET_COMPARE(&M2_TIMER,M2_CHANNEL,speed);
					HAL_TIM_PWM_Start(&M2_TIMER, M2_CHANNEL);
					break;
			case 3 : __HAL_TIM_SET_COMPARE(&M3_TIMER,M3_CHANNEL,speed);
					HAL_TIM_PWM_Start(&M3_TIMER, M3_CHANNEL);
					break;
			case 4 : __HAL_TIM_SET_COMPARE(&M4_TIMER,M4_CHANNEL,speed);
					HAL_TIM_PWM_Start(&M4_TIMER, M4_CHANNEL);
					break;
			case 5 : __HAL_TIM_SET_COMPARE(&M5_TIMER,M5_CHANNEL,speed);
					HAL_TIM_PWM_Start(&M5_TIMER, M5_CHANNEL);
					break;
			case 6 : __HAL_TIM_SET_COMPARE(&M6_TIMER,M6_CHANNEL,speed);
					HAL_TIM_PWM_Start(&M6_TIMER, M6_CHANNEL);
					break;
			case 7 : __HAL_TIM_SET_COMPARE(&M7_TIMER,M7_CHANNEL,speed);
					HAL_TIM_PWM_Start(&M7_TIMER, M7_CHANNEL);
					break;
			case 8 : __HAL_TIM_SET_COMPARE(&M8_TIMER,M8_CHANNEL,speed);
					HAL_TIM_PWM_Start(&M8_TIMER, M8_CHANNEL);
					break;
			default : break;
		}
	}

}






























































